<template>
  <div class="reply">
    <van-cell>
      <div class="item">
        <van-image
          width="35"
          height="35"
          :src="currentCom.aut_photo"
          round
          fit="cover"
        />
        <div class="msg">
          <div class="name">{{ currentCom.aut_name }}</div>
          <div class="com">{{ currentCom.content }}</div>
          <div class="other">
            <span class="time">{{ currentCom.pubdate | timefromnow }}</span>
          </div>
        </div>
      </div>
    </van-cell>
    <van-divider
      :style="{
        color: '#1989fa',
        borderColor: '#1989fa',
        padding: '0 .4267rem',
      }"
    >
      全部回复
    </van-divider>

    <van-list
      @load="onLoad"
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
    >
      <van-cell v-for="(item, index) in list" :key="index">
        <div class="item">
          <van-image
            width="35"
            height="35"
            :src="item.aut_photo"
            round
            fit="cover"
          />
          <div class="msg">
            <div class="name">{{ item.aut_name }}</div>
            <div class="com">{{ item.content }}</div>
            <div class="other">
              <span class="time">{{ item.pubdate | timefromnow }}</span>
            </div>
          </div>
        </div>
      </van-cell>
    </van-list>

    <!-- 底部输入框 -->
    <van-field
      @keyup.enter="sendCom()"
      class="myfield"
      v-model="content"
      clearable
      placeholder="请输入评论"
    >
      <template #button>
        <van-button @click="sendCom" size="small" type="info">发布</van-button>
      </template>
    </van-field>
  </div>
</template>

<script>
import { getCommentsAPI, setCommentsAPI } from "@/api";
export default {
  props: ["currentCom"],
  data() {
    return {
      list: [],
      loading: false,
      finished: false,
      content: "",
      offset: undefined,
      end_id: 0,
      limit: 10,
    };
  },

  methods: {
    async onLoad() {
      const res = await getCommentsAPI({
        type: "c",
        source: this.currentCom.com_id,
        offset: this.offset,
        limit: this.limit,
      });
      //   console.log(res)
      this.list = [...this.list, ...res.data.data.results];
      this.offset = res.data.data.last_id;
      this.end_id = res.data.data.end_id;
      this.loading = false;
      if (this.offset === this.end_id) {
        this.finished = true;
      }
    },

    async sendCom() {
      const res = await setCommentsAPI({
        target: this.currentCom.com_id,
        content: this.content,
        art_id: this.$route.query.id,
      });
      this.$toast.success("评论成功");
      // 更新评论
      this.list.unshift(res.data.data.new_obj);
      this.content = "";
      //   将当前评论回复的数量加1
      this.currentCom.reply_count += 1;
    },
  },
};
</script>

<style lang="less">
.reply {
  margin-bottom: 56px;
  .myfield {
    position: fixed;
    bottom: 0;
    left: 0;
  }
}
</style>